package cn.tedu.medicalmanagementsystem.manager.controller;


import cn.tedu.medicalmanagementsystem.base.response.JsonResult;
import cn.tedu.medicalmanagementsystem.base.response.StatusCode;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.AddNoticeParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.NoticeQuery;
import cn.tedu.medicalmanagementsystem.manager.pojo.dto.UpdateNoticeParam;
import cn.tedu.medicalmanagementsystem.manager.pojo.vo.NoticeVO;
import cn.tedu.medicalmanagementsystem.manager.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "公告管理模块")
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    @GetMapping("notices")
    @ApiOperation("公告查询")
    public JsonResult listNotice(@ApiIgnore HttpSession session) {
        List<NoticeVO> noticeList = noticeService.getNoticeList();
        log.debug("管理员查询公告数据，共获取{}条记录", noticeList.size());
        return JsonResult.ok(noticeList);
    }

    @GetMapping("addViewCount")
    @ApiOperation("公告浏览次数添加")
    public JsonResult addViewCount(@RequestParam Long id){
        try {
            noticeService.addViewCount(id);
            return JsonResult.ok();
        } catch (Exception e) {
            return new JsonResult(StatusCode.OPERATION_FAILED, e.getMessage());
        }
    }

    @PostMapping("insert")
    @ApiOperation("公告添加")
    public JsonResult insertNotice(@RequestBody AddNoticeParam addNoticeParam){
        try {
            noticeService.addNotice(addNoticeParam);
            return JsonResult.ok();
        } catch (Exception e) {
            return new JsonResult(StatusCode.OPERATION_FAILED, e.getMessage());
        }
    }

    @PostMapping("update")
    @ApiOperation("公告修改")
    public JsonResult updateNotice(@RequestBody UpdateNoticeParam updateNoticeParam){
        try {
            noticeService.updateNotice(updateNoticeParam);
            return JsonResult.ok("修改成功");
        } catch (Exception e) {
            return new JsonResult(StatusCode.OPERATION_FAILED, e.getMessage());
        }
    }

    @PostMapping("delete")
    @ApiOperation("公告删除")
    public JsonResult deleteNotice(@RequestParam Long id){
        try {
            noticeService.deleteNoticeById(id);
            return JsonResult.ok("删除成功");
        } catch (Exception e) {
            return new JsonResult(StatusCode.OPERATION_FAILED, e.getMessage());
        }
    }

    @PostMapping("select")
    @ApiOperation("特定公告查询")
    public JsonResult selectNotice(@RequestBody NoticeQuery noticeQuery){
        try {
            NoticeVO noticeVO = noticeService.selectNotice(noticeQuery);
            return JsonResult.ok(noticeVO);
        } catch (Exception e) {
            return new JsonResult(StatusCode.OPERATION_FAILED, e.getMessage());
        }
    }
}

